<?php
namespace app\controller\admin\module\index;

use app\controller\admin\module\AdminBaseController;
use app\controller\admin\module\index\components\Statistics;
use Exception;
use think\response\View;

/**
 * 首页
 * @auth-name 后台首页
 * @auth-alias index
 */
class Index extends AdminBaseController
{
    /**
     * 主页展示
     * @auth-name 主页展示
     * @auth-alias index
     * @return View
     */
    public function index(): View
    {
        $condition = ['status' => 1, 'show' => 1, 'level' => [1,2]];
        if($this->admin['is_super'] != 1){
            $mid = Model('gadminMenu')->getList(['gid' => $this->admin['gid']], 'mid');
            $condition['id'] = array_merge((!empty($mid) ? array_column($mid, 'mid') : []), [$this->getHomePageID()]);
        }
        $menuList = Model('menu')->getList($condition, 'id, pid, icon, name, module, method, method_alias, level, is_home_page, sort', 'level asc') ? : [];
        array_multisort(array_column($menuList, 'is_home_page'), SORT_DESC, array_column($menuList, 'level'), SORT_ASC,  array_column($menuList, 'sort'), SORT_DESC, $menuList);
        foreach($menuList as $k => $v){
            if(!empty($v['module'])){
                $menuList[$k]['location'] = '/admin/' . $v['module'] . (!empty($v['method']) ? '/' . ($v['method_alias'] ? : strtolower($v['method'])) : '');
            }
        }
        return fetch('/admin/index/index', ['makeMenuHtml' => $this->makeMenuHtml($this->makeMenu($menuList))]);
    }

    /**
     * 首页
     * @auth-name 首页
     * @auth-alias home
     * @return View
     * @throws Exception
     */
    public function home(): View
    {
        return fetch('/admin/index/home', [
            'total' => [
                'user' => [
                    'today' => Statistics::getInstance()->getTodayRegisterUserNumber(),
                    'total' => Statistics::getInstance()->getTotalRegisterUserNumber(),
                ],
                'room' => [
                    'today' => Statistics::getInstance()->getTodayRoomNumber(),
                    'total' => Statistics::getInstance()->getTotalRoomNumber(),
                ],
            ],
            'chartRawData' => [
                'labels' => [
                    'week' => Statistics::getInstance()->getDate('week', true)[0],
                    'month' => Statistics::getInstance()->getDate('month', true)[0],
                    'year' => Statistics::getInstance()->getDate('year', true)[0],
                ],
                'data' => [
                    'user' => [
                        'week' => Statistics::getInstance()->initializationList('week', 'user', 'createtime', 'id, createtime')->corporealList(),
                        'month' => Statistics::getInstance()->initializationList('month', 'user', 'createtime', 'id, createtime')->corporealList(),
                        'year' => Statistics::getInstance()->initializationList('year', 'user', 'createtime', 'id, createtime')->corporealList(),
                    ],
                    'room' => [
                        'week' => Statistics::getInstance()->initializationList('week', 'room', 'addtime', 'id, addtime')->corporealList(),
                        'month' => Statistics::getInstance()->initializationList('month', 'room', 'addtime', 'id, addtime')->corporealList(),
                        'year' => Statistics::getInstance()->initializationList('year', 'room', 'addtime', 'id, addtime')->corporealList(),
                    ],
                ],
            ],
        ]);
    }

    /**
     * 创建菜单Html
     * @param $list     array   菜单列表
     * @param $level    int     菜单等级
     * @return string
     */
    private function makeMenuHtml(array $list = [], int $level = 1): string
    {
        $makeMenuHtml = '';
        if(!empty($list)){
            $level != 1 and $makeMenuHtml .= '<ul class="nav nav-subnav">';
            foreach ($list as $k => $v){
                $isChild = array_key_exists('_child', $v);
                $makeMenuHtml .= '<li class="nav-item '. ($isChild ? 'nav-item-has-subnav' : '') . ' ' . ($k == 0 ? 'active' : '') . '">';
                $makeMenuHtml .= !$isChild ? '<a class="multitabs" ' . ($v['is_home_page'] == 1 ? 'id="default-page"' : '') . ' href="' . ((isset($v['location']) and !empty($v['location'])) ? url($v['location']) : '') .'">' : '<a href="javascript:void(0)">';
                ($level == 1 and !empty($v['icon'])) and $makeMenuHtml .= '<i class="' . $v['icon'] . '"></i>';
                $makeMenuHtml .= $level != 1 ? $v['name'] : '<span>' . $v['name'] . '</span>';
                $makeMenuHtml .= '</a>';
                if($isChild){
                    $makeMenuHtml .= $this->makeMenuHtml($v['_child'], $v['level']+1);
                }
                $makeMenuHtml .= '</li>';
            }
            $level != 1 and $makeMenuHtml .= '</ul>';
        }
        return $makeMenuHtml;
    }

    /**
     * 创造菜单列表
     * @param array $list
     * @return array
     */
    private function makeMenu(array $list): array
    {
        $tree = [];
        $packData = array_column($list, null, 'id');
        foreach ($packData as $key => $val) {
            if ($val['pid'] == 1) {
                //代表跟节点, 重点一
                $tree[] = &$packData[$key];
            } else {
                //找到其父类,重点二
                $packData[$val['pid']]['_child'][] = &$packData[$key];
            }
        }
        return $tree;
    }

    /**
     * 获取首页的id
     * @return int
     */
    private function getHomePageID(): int
    {
        return Model('menu')->getInfo(['is_home_page' => 1], 'id') ? : 0;
    }

}
